<?php

if ($table) {
	$table_tag = 'table';
	$thead_tag = 'thead';
	$tbody_tag = 'tbody';
	$tr_tag = 'tr';
	$th_tag = 'th';
	$td_tag = 'td';
} else {
	$table_tag = $thead_tag = $tbody_tag = 
		$tr_tag = $th_tag = $td_tag = 'div';
}
echo "<$table_tag>\r\n\t<$thead_tag>\r\n\t\t<$tr_tag>\r\n";
$cols = array();
$last_cell = count($columns) - 1;
$idx_cell = 0;
if ($select || $multiselect) {
	echo "			<th class=\"f-even f-grid-cell-select\"></th>\r\n";
}
foreach ($columns as $name => $col_data) {
	if (is_string($col_data)) $col_data = array('id' => $name, 'label' => $col_data);
	else $col_data['id'] = $name;
	$cols[] = $col_data;
	echo '			<th class="f-'.($idx_cell % 2 ? 'odd' : 'even');
		if ($idx_cell == 0) echo ' f-first';
		if ($idx_cell == $last_cell) echo ' f-last';
		echo ' f-n'.$idx_cell;
		echo '">'.htmlspecialchars($col_data['label'])."</th>\n";
		$idx_cell++;
}
if (isset($buttons) || isset($toolbar)) {
	echo '			<th class="f-'.($idx_cell % 2 ? 'odd' : 'even').
		' f-grid-toolbar">';
	if (isset($toolbar)) foreach ($toolbar as $button_id => $button) {
		$this->render('grid.button',array_merge($button,
			array('id' => $button_id,'disabled' => $disabled)));
	}
	echo "</th>\r\n";
}
echo "\r\n\t\t</$tr_tag>\r\n\t</$thead_tag>\r\n\t<$tbody_tag>\r\n";
$column_names = array_keys($columns);
$last_row = count($data) - 1;
$idx_row = 0;
foreach ($data as $row_id => $row) {
	if (is_integer($row_id) && is_object($row) && isset($row->_f_pkey_field_)) {
		$pkfield = $row->_f_pkey_field_;
		$key = $row->$pkfield;
	} else {
		$key = $row_id;
	}
	$key = urlencode($key);
	echo "\t\t<$tr_tag class=\"f-";
	echo $idx_row % 2 ? 'odd' : 'even';
	if ($idx_row == 0) echo ' f-first';
	if ($idx_row == $last_row) echo ' f-last';
	echo "\">\r\n";
 	$even = true;
	if ($select || $multiselect) { 
		echo "\t\t\t<".$td_tag.' class="f-even f-grid-cell-select"><input type="'.
		($multiselect ? 'checkbox' : 'radio').'" name="'.$id.'[selected][]" id="'.
		$id.'-'.$key.'" value="'.$key.'"'.($disabled ? ' disabled="disabled"' : '').
		'/></'.$td_tag.">\r\n";
		$even = false;
	}
	for ($idx_cell = 0; $idx_cell <= $last_cell; $idx_cell++) {
		$col = $cols[$idx_cell];
		$editable = isset($col['editable']) && $col['editable'];
		$column_name = $column_names[$idx_cell];
		if (is_array($row)) {
			$cell = $row[$idx_cell];
		} else {
			$cell = is_array($row) ? (isset($row[$idx_cell]) ? $row[$idx_cell] : '') :
				(isset($row->$column_name) ? $row->$column_name : '');
		}
		echo "\t\t\t<".$td_tag.' class="f-'.($even ? 'even' : 'odd');
		if ($idx_cell == 0) echo ' f-first';
		if ($idx_cell == $last_cell) echo ' f-last';
		if ($editable) echo ' f-grid-cell-editable';
		echo ' f-n'.$idx_cell;
		echo '">';
		if (isset($col['renderer'])) {
			echo call_user_func($col['renderer'],$cell,$col);
		} else {
			if ($editable) echo '<input name="'.$id.
				'[values]['.$row_id.']['.$col['id'].']" value="';
			echo isset($col['html']) && $col['html'] ? $cell : htmlspecialchars($cell);
			if ($editable) echo '"/>';
		}
		echo "</$td_tag>\n";
		$even = !$even;
	}
	if (isset($buttons)) {
		echo "\t\t\t<".$td_tag.' class="f-'.($even ? 'even' : 
			'odd').' f-grid-row-buttons">';
		foreach ($buttons as $button_id => $button) {			
			$this->render('grid.button',array_merge($button,
				array('id' => $button_id,'disabled' => $disabled,
				'key' => $key)));
		}
		echo "</$td_tag>";
	}
	echo "\r\n\t\t</$tr_tag>\r\n";
	$idx_row++;
}
echo "\r\n\t</$tbody_tag>\r\n</$table_tag>";
